﻿// Copyright (c) Kona Framework Team. All rights reserved.
// For continued development and updates, visit http://www.codeplex.com/kona
// For a copy of the license please vist: http://www.codeplex.com/kona/license

using System.Xml;
using AdomdClient = Microsoft.AnalysisServices.AdomdClient;
using System.Data;

namespace Kona.Data
{
    public class AdoMdCommand:System.ComponentModel.Component,IDbCommand
    {
        private readonly AdomdClient.AdomdCommand internalCommand;

        public AdoMdCommand()
        {
            internalCommand = new AdomdClient.AdomdCommand();
        }

        public AdoMdCommand(string commandText):this()
        {
            internalCommand.CommandText= commandText;
        }

        public AdoMdCommand(string commandText,AdoMdConnection connection):this(commandText)
        {
            this.Connection = connection;
        }

        public AdoMdConnection Connection
        { get; set; }


        public XmlReader ExecuteXmlReader()
        {
            this.internalCommand.Connection = this.Connection.InternalConnection;
            return this.internalCommand.ExecuteXmlReader();
        }

        #region Implementation of IDbCommand

        public void Prepare()
        {
            this.internalCommand.Connection = this.Connection.InternalConnection;
            this.internalCommand.Prepare();
        }

        public void Cancel()
        {
            this.internalCommand.Cancel();
        }

        public int ExecuteNonQuery()
        {
            this.internalCommand.Connection = this.Connection.InternalConnection;
            return this.internalCommand.ExecuteNonQuery();
        }

        public IDataReader ExecuteReader()
        {
            this.internalCommand.Connection = this.Connection.InternalConnection;
            return this.ExecuteReader();
        }

        public IDataReader ExecuteReader(CommandBehavior behavior)
        {
            this.internalCommand.Connection = this.Connection.InternalConnection;
            return this.internalCommand.ExecuteReader(behavior);
        }

        public object ExecuteScalar()
        {
            throw new System.NotSupportedException();
        }

        IDbConnection IDbCommand.Connection
        {
            get { return this.Connection; }
            set { this.Connection = (AdoMdConnection) value; }
        }

        IDbTransaction IDbCommand.Transaction
        {
            get { throw new System.NotImplementedException(); }
            set { throw new System.NotImplementedException(); }
        }

        IDataParameterCollection IDbCommand.Parameters
        {
            get { return this.internalCommand.Parameters; }
        }

        IDbDataParameter IDbCommand.CreateParameter()
        {
            return new AdomdClient.AdomdParameter();
        }

        public string CommandText
        {
            get { return this.internalCommand.CommandText; }
            set { this.internalCommand.CommandText = value; }
        }

        public int CommandTimeout
        {
            get { return this.internalCommand.CommandTimeout; }
            set { this.internalCommand.CommandTimeout = value; }
        }

        public CommandType CommandType
        {
            get { return this.internalCommand.CommandType; }
            set { this.CommandType = value; }
        }

        public UpdateRowSource UpdatedRowSource
        {
            get
            {
                throw new System.NotSupportedException();
            }
            set
            {
                throw new System.NotSupportedException();
            }
        }

        #endregion
    }
}
